/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick.controller;

import com.appspot.ittrick.drive.trick.DriveTrick;
import com.appspot.ittrick.drive.trick.db.AccountMstEntity;
import com.appspot.ittrick.drive.trick.db.CookiesObjectEntity;
import com.appspot.ittrick.drive.trick.db.DBManager;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebView;

/**
 *
 * @author takahara
 */
public class FileWebViewController extends AnchorPane implements Initializable {
    
    @FXML
    private WebView fileWebView;
    /**
     * ProgressIndicator
     */
    @FXML
    public HBox progressIndicatorHBox;
    /**
     * load url
     */
    private String loadUrl;
    private String accsessToken;

    /**
     * コンストラクタ
     */
    public FileWebViewController(String loadUrl, String accsessToken) {
        this.loadUrl = loadUrl.replace("?usp=drivesdk", "");
        this.loadUrl = loadUrl.replace("&usp=drivesdk", "");
        if(this.loadUrl.indexOf("?") != -1) {
            this.loadUrl = this.loadUrl + "&";
        }else {
            this.loadUrl = this.loadUrl + "?";
        }
        this.loadUrl = this.loadUrl + "access_token=" + accsessToken;
        this.accsessToken = accsessToken;
        loadFXML();
    }

    /**
     * 初期化メソッド
     */
    private void loadFXML() {
        // ---------------------------------------------------
        // FXMLのロードと設定
        // ---------------------------------------------------
        FXMLLoader fxmlLoader = new FXMLLoader(DriveTrick.class.getResource("fxml/file_webview.fxml"));
        fxmlLoader.setRoot(this);
        // 自分自身をコントロールとして設定
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    /**
     * initialize
     * @param url
     * @param rb 
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
//        try {
            progressIndicatorHBox.visibleProperty().bind(fileWebView.getEngine().getLoadWorker().runningProperty());
            
    //        try {
    //            AccountMstEntity selectAccount = DBManager.getSelectAccountInfo(DBManager.getConn());
    //            ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(selectAccount.cookiesObject));
    //            List<CookiesObjectEntity> cookiesList = (List)objectInputStream.readObject();
    //            
    ////            // 新しいCookieManagerを生成
    ////        final CookieManager manager = new CookieManager();
    ////        // すべての Cookie を受けいれる
    ////        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    ////        // CookieHandler のデフォルトに設定
    ////        CookieHandler.setDefault(manager);
    //        
    //            URI uri = new URI("http://docs.google.com");
    //            List<String> cookiesHeaderList = new ArrayList();
    //            for (CookiesObjectEntity entity : cookiesList) {
    //                System.out.println(entity.getName()+ ":" + entity.getValue());
    //                cookiesHeaderList.add(entity.getName()+ "=" + entity.getValue());
    //            }
    //            
    //            Map<String, List<String>> headers = new LinkedHashMap<>();
    //            headers.put("Set-Cookie", cookiesHeaderList);
    //            CookieHandler.getDefault().put(uri, headers);
    //            
    //        } catch (SQLException | IOException | ClassNotFoundException | URISyntaxException ex) {
    //            Logger.getLogger(FileWebViewController.class.getName()).log(Level.SEVERE, null, ex);
    //        }
            
//            AccountMstEntity accountMstEntity = DBManager.getAccountInfo(DBManager.getConn(), mailAddress);
            
//            Map<String, List<String>> headers = new LinkedHashMap<>();
//            headers.put("Set-Cookie", Arrays.asList("Authorization: Bearer " + accsessToken));
//            java.net.CookieHandler.getDefault().put(new URI("http://docs.google.com"), headers);

        System.out.println("https://docs.google.com/spreadsheet/ccc?key=0Ao9BTQSLJPpMdHFQbWJWNE1fMXNJY2N1UVlXbjdtLWc&access_token=ya29.AHES6ZSafNCGKVsXdDiuoZOfMOPsnIkMmjn2_UGoDIzVNnFdHSU");
        fileWebView.getEngine().setJavaScriptEnabled(true);

        CookieManager manager = new CookieManager();
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(manager);
        
            fileWebView.getEngine().load(loadUrl);
            
            System.out.println(loadUrl);
        
    }
}
